<?php

namespace app\common\model\search;

use MongoDB\BSON\ObjectId;
use app\common\model\PatientInfo as PatientInfoModel;

trait FollowSearch
{
    //姓名
    public function searchNameAttr($query, $value, $data)
    {
        return $query->where('name', 'like', trim($value));
    }

    //病案号
    public function searchMedicalRecordsNoAttr($query, $value, $data)
    {
        
        $row = (new PatientInfoModel)->where('medicalRecordsNo', trim($value))->field('id')->find();

        $id = isset($row)?$row['id']:'';
        
        return $query->where('pid', $id);
      
    }

    //随访状态
    public function searchStatusAttr($query, $value, $data)
    {
        $where = [];
        if ($value == 0)
        {
            $where[] = ['followStatus', '<>', 1];
        } elseif ($value == 1){
            $where[] = ['followStatus', '=', (int)$value];
        }
        return  $query->where($where);
    }

    public function searchIdAttr($query, $value, $data)
    {
        return $query->where('task_id', new ObjectId($value));
    }

    //电话号
    public function searchMobileAttr($query, $value, $data)
    {
        return $query->where('presentTelphone', trim($value));
    }

    //随访结果
    public function searchResultAttr($query, $value, $data)
    {
        return $query->where('result', (int)$value);
    }

    //随访状态
    public function searchFollowStatusAttr($query, $value, $data)
    {
        return $query->where('followStatus', (int)$value);
    }

    //疾病
    public function searchDiagnosisAttr($query, $value, $data)
    {
        return $query->where('diagnosis', trim($value));
    }

    //病种
    public function searchBzAttr($query, $value, $data)
    {
        $where = [];
        if (!empty($value)) {
            $pids = (new PatientInfoModel())->where([["bz.bingzhongArr.bingzhong_id", "in", $value]])->column('id');
            $where[] = ['pid', 'in', $pids];
        }
        return $query->where($where);
    }

    //随访人员
    public function searchFollowStaffAttr($query, $value, $data)
    {
        return $query->where('follow_staff.doctor_code', $value);
    }

    //是否在院
    public function searchIsInHospitalAttr($query, $value, $data)
    {

        $patientId = $this->where('task_id', new ObjectId($data['id']))->column('patientId');

        if ($value == 1) {
            $where = ['is_in_hospital', '=', 1];
        }else{
            $where = ['is_in_hospital', '<>', 1];
        }

        $is_in_hospital = (new PatientInfoModel())->where([$where,["patientId", "in", $patientId]])->column('patientId');
        return $query->whereIn('patientId', $is_in_hospital);
    }

    //分娩开始日期
    public function searchStartBirthDateAttr($query, $value, $data)
    {
        return $query->where([['time_of_birth', '>=', $value]]);
    }

    //分娩结束日期
    public function searchEndBirthDateAttr($query, $value, $data)
    {
        return $query->where([['time_of_birth', '<=', $value]]);
    }

    //是否暂存
    public function searchIsStagingAttr($query, $value, $data)
    {
        $where = [];
        $where2 = [];
        switch ($value) {
            case 1:
                $where[] = ['staging_status', '=', 1];
                $patientIds = (new PatientInfoModel())->where($where)->column('patientId');
                $where2[] = ['patientId', 'in', $patientIds];
                break;
            case 2:
                $where[] = ['staging_status', '=', 1];
                $patientIds = (new PatientInfoModel())->where($where)->column('patientId');
                $where2[] = ['patientId', 'not in', $patientIds];
                break;
            default:
                break;
        }
        return $query->where($where2);
    }
}
